import { useEventHub } from '@/hooks/useEventHub';
import {createRouter, createWebHistory, RouteLocationNormalized} from 'vue-router';
import { routes } from './routes';
const hub = useEventHub();
const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior(to){
        if (to.hash){
            return {
                el: to.hash,
                behavior: 'smooth'
            }
        }
        return {
            top: 0,
            left: 0,
            behavior: 'smooth'
        }
    }
})
router.beforeEach((to: RouteLocationNormalized)=>{
    const {meta: {type, title}} = to;
    const defaultTitle = '灯塔'
    document.title = `${defaultTitle + (title ? '-' + title : '')}`;
    hub.emit('menu::update', type);
})

export default router;